public without sharing class PurchaseDemandPlanDetailHandler {
    

//     //导入明细数据时自动找主表,没有则生成主表
// 	public static void matchMain(List<PurchaseDemandPlanDetail__c> list_new){

// 		Set<String> years = new Set<String>();
// 		Set<String> months = new Set<String>();
// 		Set<String> companys = new Set<String>();
// 		Set<String> provinces = new Set<String>();
// 		Map<String,Id> productMap = new Map<String,Id>();
// 		Map<String,List<PurchaseDemandPlanDetail__c>> detailMap = new Map<String,List<PurchaseDemandPlanDetail__c>>();
// 		for (PurchaseDemandPlanDetail__c pd  : list_new ) {
			
// 			years.add(pd.Year__c);
// 			months.add(pd.Month__c);
// 			companys.add(pd.Company__c);
// 			provinces.add(pd.Province__c);
// 			productMap.put(pd.NCCode__c, null);
// 			String key = pd.Year__c+pd.Month__c +pd.Company__c +pd.Province__c;

// 			List<PurchaseDemandPlanDetail__c> tmp = detailMap.get(key);
// 			if(tmp == null)tmp = new List<PurchaseDemandPlanDetail__c> ();

// 			tmp.add(pd);
// 			detailMap.put(key, tmp);
// 		}

// 		//匹配产品
// 		for (Product__c p  :[select Id ,NCCode__c from Product__c where RecordType.DeveloperName = 'NCProduct' and NCCode__c in: productMap.keySet()] ) {
// 			productMap.put(p.NCCode__c, p.Id);
// 		}
// 		//匹配主表
// 		Map<String,PurchaseDemandPlan__c > pMap = new Map<String,PurchaseDemandPlan__c >();
// 		for (PurchaseDemandPlan__c p  :[select Id , Year__c,Month__c,Company__c,Province__c 
// 			from PurchaseDemandPlan__c where Year__c in: years and Month__c in:months and Company__c in:companys and Province__c in:provinces] ) {
// 			String key = p.Year__c+p.Month__c +p.Company__c +p.Province__c;
// 			pMap.put( key,p);


// 		}

// 		for (String k  : detailMap.keySet() ) {
			
// 			if(!pMap.containsKey(k)){
// 				PurchaseDemandPlan__c p = new PurchaseDemandPlan__c();
// 				p.Year__c = detailMap.get(k).get(0).Year__c;
// 				p.Month__c = detailMap.get(k).get(0).Month__c;
// 				p.Company__c = detailMap.get(k).get(0).Company__c;
// 				p.Province__c = detailMap.get(k).get(0).Province__c;
// 				pMap.put(k, p);

// 			}
// 		}
// 		//更新/创建主表
// 		upsert pMap.values();
// 		//匹配主表 Id 和产品 Id 
// 		for (String k  : detailMap.keySet()) {
			
// 			 PurchaseDemandPlan__c p  = pMap.get(k);
// 			 for(PurchaseDemandPlanDetail__c pd : detailMap.get(k)){
// 			 	pd.Product__c = productMap.get(pd.NCCode__c);
// 			 	pd.PurchaseDemandPlan__c = p.Id;
// 			 }

// 		}


// 	}


}